package com.servlet.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * @Title: MySessionAttributeListener
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/26 13:30
 */
@WebListener
public class MySessionAttributeListener implements HttpSessionAttributeListener {

    /**
     * 添加的时候触发
     * @param httpSessionBindingEvent
     */
    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        String name = httpSessionBindingEvent.getName();
        Object value = httpSessionBindingEvent.getValue();
        System.out.println("session 添加了属性...."+name+":"+value);
    }

    /**
     * 移除的时候触发
     * @param httpSessionBindingEvent
     */
    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("session 移除了属性....");
    }

    /**
     * 替换的时候触发
     * @param httpSessionBindingEvent
     */
    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        Object beforeValue  = httpSessionBindingEvent.getValue();
        Object afterValue = httpSessionBindingEvent.getSession().getAttribute(httpSessionBindingEvent.getName());
        System.out.println("session 替换了属性....替换之前"+beforeValue+":"+afterValue);
    }
}
